package com.hc.componentspringboot.controller;

import com.hc.componentspringboot.bean.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

//@RestController
@Controller //返回页面时不能使用@RestController
@RequestMapping("test")
public class TestController {

    //------------------接收非json数据----------------
    @RequestMapping("f1") //请求参数名与形参一致 test/f1?username=zhangsan&password=123
    public String f1(String username,String password){
        return "success";
    }

    @RequestMapping("f2") //请求参数名与形参不一致 test/f2?name=zhangsan&pwd=123
    public String f2(@RequestParam("name") String username, @RequestParam("pwd") String password){
        return "success";
    }

    @RequestMapping("f3") //使用对象形参接收 test/f2?username=zhangsan&password=123
    public String f3(User user){
        return "success";
    }

    @RequestMapping("f4/{username}/{pwd}") //接收restful请求test/f4/zhangsan/123 后端请求地址参数与形参一致@PathVariable值可省略
    public String f4(@PathVariable String username, @PathVariable("pwd") String password){
        return "success";
    }

    @RequestMapping("f5") //借助HttpServletRequest获取
    public String f5(HttpServletRequest request){
        //request.getParameter("");
        return "success";
    }
    //------------------接收json数据----------------
    @RequestMapping("f6") //@RequestBody表明请求提交的数据是json格式的
    public String f6(@RequestBody User user){
        return "success";
    }
    //------------------返回页面------------------
    @RequestMapping("f7")
    public String f7(Model model){
        //绑定数据
//        model.addAttribute("year",2025);
        return "login";
    }
    //-----------------引用SpringSecurity,获取用户信息方式-----------------
    public void f8(HttpSession session){
        //HttpSession获取用户信息
        Enumeration<String> enumeration = session.getAttributeNames();
        while (enumeration.hasMoreElements()){
            String key = enumeration.nextElement();
            //安全框架封装的类
            SecurityContextImpl value = (SecurityContextImpl) session.getAttribute(key);
            Authentication authentication = value.getAuthentication();
            UserDetails userDetails = (UserDetails) authentication.getDetails();
            String username = userDetails.getUsername();
            String password = userDetails.getPassword();
        }
    }

    public void f8(){
        //SecurityContextHolder获取用户信息
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getDetails();
        String username = userDetails.getUsername();
        String password = userDetails.getPassword();
    }
}
